

/**
 * Created by IntelliJ
 * Author:熊磊
 */
// TODO 运算没有从往右进行
public class Calculator {
    public static void main(String[] args) {
        String expression = "5+70*10*10+5-4";
        ArrayStack1 numStack=new ArrayStack1(10);
        ArrayStack1 operStack=new ArrayStack1(10);
        int index=0;
        int num1=0;
        int num2=0;
        int oper=0;
        int res=0;
        char ch = ' ';
        String keepNum = "";
        while (index<expression.length()){
            ch =expression.charAt(index++);
            if (isOper(ch)){
                if (operStack.isEmpty()){
                    operStack.push(ch);
                } else {
                    if (priority(ch) <= priority(operStack.peek())){
                        num1=numStack.pop();
                        num2=numStack.pop();
                        oper=operStack.pop();
                        res=cal(num1,num2,oper);
                        numStack.push(res);
                        operStack.push(ch);
                    }else {
                        operStack.push(ch);
                    }
                }
            } else {
                //判断下一个字符还是不是数字
                keepNum+=ch;
                while (true){
                    if (index>=expression.length()){
                        break;
                    } else {
                        ch=expression.charAt(index);
                        if (isOper(ch)){
                            break;
                        }
                        keepNum+=ch;
                        index++;
                    }
                }
                //ASCII码转换
                numStack.push(Integer.parseInt(keepNum));
                //情空keepnum
                keepNum="";
            }
        }
        while (!operStack.isEmpty()){
            num1=numStack.pop();
            num2=numStack.pop();
            oper=operStack.pop();
            res=cal(num1,num2,oper);
            numStack.push(res);
        }
        System.out.println(numStack.pop());
    }
    //返回运算符的优先级,数字越大则优先级越高
    public static int priority(int oper){
        if (oper== '*' || oper=='/'){
            return 1;
        } else if (oper== '+' || oper=='-'){
            return 0;
        } else {
            throw new RuntimeException("运算符出错!");
        }
    }
    //判断是否为运算符
    public static boolean isOper(char val){
        return val=='+' || val=='-' || val=='*' || val=='/';
    }

    //计算方法
    public static int cal(int num1,int num2,int oper){
        int res=0;
        switch (oper){
            case '+':
                res = num2+num1;
                break;
            case '-':
                res = num2-num1;
                break;
            case '*':
                res = num2*num1;
                break;
            case '/':
                res = num2/num1;
                break;
            default:
                break;
        }
        return res;
    }
}
class ArrayStack1 {
    private int maxSize;
    private int[] stack;
    private int top = -1;

    public ArrayStack1(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[maxSize];
    }

    //判断栈是否满了
    public boolean isFull() {
        return top == maxSize - 1;
    }

    //判断栈空
    public boolean isEmpty() {
        return top == -1;
    }

    //入栈
    public void push(int value) {
        if (isFull()) {
            System.out.println("栈满了!!!");
        } else {
            top++;
            stack[top] = value;
        }
    }

    //出栈
    public int pop() {
        if (isEmpty()) {
            throw new RuntimeException("栈为空!!!!");
        } else {
            int value = stack[top];
            top--;
            return value;
        }
    }

    //遍历栈
    public void list() {
        if (isEmpty()) {
            System.out.println("没有数据");
        } else {
            System.out.println("==============================");
            for (int i = top; i >= 0; i--) {
                System.out.printf("stack[%d]=%d\n", i, stack[i]);
            }
            System.out.println("==============================");
        }
    }

    public int peek(){
        return stack[top];
    }
}